/*
 * Function test for parsing enveloped data using glib framework
 *
 * Copyright (c) Huawei Technologies Co., Ltd. 2022. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */
#include "kms.h"
#include "cms.h"
#include <glib.h>
#include <stdint.h>
#include <openssl/bio.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>

/* Enveloped Data from KMS */
static unsigned char enveloped_data_enbase64[] =
    "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";

static unsigned char rsa_private_key[] =
    "-----BEGIN RSA PRIVATE KEY-----\n"
    "MIIEowIBAAKCAQEAmp5/oY74RfaEHV4bn2mXrbKRH6f0NcSENXzPF4mSK6xnGfD/\n"
    "9vS8zNsrUsnCc6EhrogpLYWUPWHLl3rJwl9ajg390Ejo9+X2phM20dcblLzZG+F8\n"
    "cRWaBlsrOMnlK8nlIAds9jGfTrJU/Bmg3uOgx5juAXDtfK/aQ1m2egFpGKoX/tVu\n"
    "vIV7XglrWtEshqxQinaNFuaCvSeg9yEPd9430rNH5l/RvK7eXOLqG+9mZhDHbf4d\n"
    "CYE0Aoxv+n5G/I+yQW3gw26ZHNT8JGb2TwmXFrjecqe70Ctxg8C0iL5/5ituVVK0\n"
    "YzQREa7qE9dEzi3bVI1/o0IfPcbV895GrVcslwIDAQABAoIBADmDn8I+5kdQDZDH\n"
    "Astngc5Qz/7QgNzHPs6OoJIG0GKCfM9/gBcirmAdXp4rArP0Sg0cBQUz3RKAIDU2\n"
    "rXop8gtJVRJ3LIGZ/weDui6QL2hCXxgp5hpCiMq4evli0GZbcWlamcNybeneGBK2\n"
    "jcqbLaFRV6cdlJl+6FeJNYFoqOlNOB1awjrjBp5GsgR2A756IRjhBnPesOeOTD/N\n"
    "hlIkYfLcIi50zOQy3to+/RodmKJ3VmQ5diTwmMWUhf4om0bbm2ruaEyWlzf8bKnb\n"
    "LsNAv2+oK054wOf0XAHRNZDOtSZOnvcYuQwRNXS6cgPe9Blsb40SYHHxlolR/GwA\n"
    "VHBkjiECgYEAyYUdHWsGm6BiU/P3Vct7OuUrlLymcjkASnlKCYMkuP36lw96Gghw\n"
    "S5OTxee7GRlFBRIlT3Y25FdCjfGXhc3tFVf878UURsOJPMVK1b1SqhpzaKyKY6HY\n"
    "OjipgWlu+deo26MiIAdk7VVlAytDuTC+TZd+GT9NLFd2WydqsH8sfy8CgYEAxGtz\n"
    "OUDOb9NbBGHffH/G4RDTGDmZrJrCwjvKEDrNfSigMN1gdptRIOt7YS8HxMhlbQJQ\n"
    "iN8viTbWP1eHUzKNq6ZnJCqTKBXsaQGQ79a2ilpuDEAYxb5pBFw8p1mnIC8GPuC8\n"
    "E7BSGG/Rx9eovLxorPQWa32fIJzXjLwlSNdlDxkCgYBEqmnoSz6eKsE2cAvYHRSp\n"
    "VctKzBk/FHMn7TowEZKiwk/vxyNI2ZVB+1BAABz9iWG+P5edAt/jYGRaL3b87dUa\n"
    "D7gtDw9AqYIKxgMg33jaY1bt1NT26//cntALO5zH3xc3d9e56CsYv59CvCxAXv6J\n"
    "O9SIz8wBDvBSURUGfN7xmwKBgQC7XZRIZR2HZqJIB+TcKjdYsmfrj+ab0/U8uzQB\n"
    "T1Z9sac9da9y9K3rLTZAelB60n6OAcs5BU2fIoQ1YBVis7KYrt9sjIBbmK4vbKMJ\n"
    "ZlQynhij7vCykPQ0iJgDO25FMRZQHKjMT0HAtcnzTDFV+JQRh7VrTCXjQjoLsd4P\n"
    "Igjw4QKBgHKEemiPuOLhUGR/sOkYNCKocOaRX/7p3JovpNDuyacr0XjOjoawbYmO\n"
    "EJxSwwEtIuPk/u00J1fAvqB/BYDGL0UEsnZD91BBYaTsZ7Ny6nXtnkxPIjFDvKpb\n"
    "2HwmpHQcg+6ZRbaQi2GlvQgYCB2s/FyiAg8HNUsdNs3FiG4tuh6F\n"
    "-----END RSA PRIVATE KEY-----\n";

static uint8_t expected_data[] = {
    0x39, 0x06, 0x74, 0x6e, 0xd5, 0xaf, 0xcc, 0x8e, 0xd1, 0x5f, 0x77, 0xc0,
    0xe3, 0xe6, 0x85, 0x41, 0xcb, 0x6c, 0xf4, 0x99, 0x19, 0x6c, 0x49, 0x69,
    0x3c, 0x02, 0xe9, 0x1f, 0x02, 0xb6, 0xc9, 0x72, 0xc5, 0xb5, 0xd2, 0x6a,
    0x76, 0xf9, 0x88, 0x9e, 0x61, 0x02, 0x0b, 0xf3, 0x39, 0xb1, 0x60, 0xd6,
    0x8e, 0xc6, 0xa0, 0xba, 0x54, 0x12, 0xde, 0x82, 0xc4, 0x0c, 0x7e, 0xdf,
    0x0e, 0x88, 0xfd, 0xce, 0x39, 0x06, 0x74, 0x6e, 0xd5, 0xaf, 0xcc, 0x8e,
    0xd1, 0x5f, 0x77, 0xc0, 0xe3, 0xe6, 0x85, 0x41, 0xcb, 0x6c, 0xf4, 0x99,
    0x19, 0x6c, 0x49, 0x69, 0x3c, 0x02, 0xe9, 0x1f, 0x02, 0xb6, 0xc9, 0x72,
    0xc5, 0xb5, 0xd2, 0x6a, 0x76, 0xf9, 0x88, 0x9e, 0x61, 0x02, 0x0b, 0xf3,
    0x39, 0xb1, 0x60, 0xd6, 0x8e, 0xc6, 0xa0, 0xba, 0x54, 0x12, 0xde, 0x82,
    0xc4, 0x0c, 0x7e, 0xdf, 0x0e, 0x88, 0xfd, 0xce, 0x39, 0x06, 0x74, 0x6e,
    0xd5, 0xaf, 0xcc, 0x8e, 0xd1, 0x5f, 0x77, 0xc0, 0xe3, 0xe6, 0x85, 0x41,
    0xcb, 0x6c, 0xf4, 0x99, 0x19, 0x6c, 0x49, 0x69, 0x3c, 0x02, 0xe9, 0x1f,
    0x02, 0xb6, 0xc9, 0x72, 0xc5, 0xb5, 0xd2, 0x6a, 0x76, 0xf9, 0x88, 0x9e,
    0x61, 0x02, 0x0b, 0xf3, 0x39, 0xb1, 0x60, 0xd6, 0x8e, 0xc6, 0xa0, 0xba,
    0x54, 0x12, 0xde, 0x82, 0xc4, 0x0c, 0x7e, 0xdf, 0x0e, 0x88, 0xfd, 0xce,
    0x39, 0x06, 0x74, 0x6e, 0xd5, 0xaf, 0xcc, 0x8e, 0xd1, 0x5f, 0x77, 0xc0,
    0xe3, 0xe6, 0x85, 0x41, 0xcb, 0x6c, 0xf4, 0x99, 0x19, 0x6c, 0x49, 0x69,
    0x3c, 0x02, 0xe9, 0x1f, 0x02, 0xb6, 0xc9, 0x72, 0xc5, 0xb5, 0xd2, 0x6a,
    0x76, 0xf9, 0x88, 0x9e, 0x61, 0x02, 0x0b, 0xf3, 0x39, 0xb1, 0x60, 0xd6,
    0x8e, 0xc6, 0xa0, 0xba, 0x54, 0x12, 0xde, 0x82, 0xc4, 0x0c, 0x7e, 0xdf,
    0x0e, 0x88, 0xfd, 0xce, 0x39, 0x06, 0x74, 0x6e, 0xd5, 0xaf, 0xcc, 0x8e,
    0xd1, 0x5f, 0x77, 0xc0, 0xe3, 0xe6, 0x85, 0x41, 0xcb, 0x6c, 0xf4, 0x99,
    0x19, 0x6c, 0x49, 0x69, 0x3c, 0x02, 0xe9, 0x1f, 0x02, 0xb6, 0xc9, 0x72,
    0xc5, 0xb5, 0xd2, 0x6a, 0x76, 0xf9, 0x88, 0x9e, 0x61, 0x02, 0x0b, 0xf3,
    0x39, 0xb1, 0x60, 0xd6, 0x8e, 0xc6, 0xa0, 0xba, 0x54, 0x12, 0xde, 0x82,
    0xc4, 0x0c, 0x7e, 0xdf, 0x0e, 0x88, 0xfd, 0xce, 0x39, 0x06, 0x74, 0x6e,
    0xd5, 0xaf, 0xcc, 0x8e, 0xd1, 0x5f, 0x77, 0xc0, 0xe3, 0xe6, 0x85, 0x41,
    0xcb, 0x6c, 0xf4, 0x99, 0x19, 0x6c, 0x49, 0x69, 0x3c, 0x02, 0xe9, 0x1f,
    0x02, 0xb6, 0xc9, 0x72, 0xc5, 0xb5, 0xd2, 0x6a, 0x76, 0xf9, 0x88, 0x9e,
    0x61, 0x02, 0x0b, 0xf3, 0x39, 0xb1, 0x60, 0xd6, 0x8e, 0xc6, 0xa0, 0xba,
    0x54, 0x12, 0xde, 0x82, 0xc4, 0x0c, 0x7e, 0xdf, 0x0e, 0x88, 0xfd, 0xce,
    0x39, 0x06, 0x74, 0x6e, 0xd5, 0xaf, 0xcc, 0x8e, 0xd1, 0x5f, 0x77, 0xc0,
    0xe3, 0xe6, 0x85, 0x41, 0xcb, 0x6c, 0xf4, 0x99, 0x19, 0x6c, 0x49, 0x69,
    0x3c, 0x02, 0xe9, 0x1f, 0x02, 0xb6, 0xc9, 0x72, 0xc5, 0xb5, 0xd2, 0x6a,
    0x76, 0xf9, 0x88, 0x9e, 0x61, 0x02, 0x0b, 0xf3, 0x39, 0xb1, 0x60, 0xd6,
    0x8e, 0xc6, 0xa0, 0xba, 0x54, 0x12, 0xde, 0x82, 0xc4, 0x0c, 0x7e, 0xdf,
    0x0e, 0x88, 0xfd, 0xce, 0x39, 0x06, 0x74, 0x6e, 0xd5, 0xaf, 0xcc, 0x8e,
    0xd1, 0x5f, 0x77, 0xc0, 0xe3, 0xe6, 0x85, 0x41, 0xcb, 0x6c, 0xf4, 0x99,
    0x19, 0x6c, 0x49, 0x69, 0x3c, 0x02, 0xe9, 0x1f, 0x02, 0xb6, 0xc9, 0x72,
    0xc5, 0xb5, 0xd2, 0x6a, 0x76, 0xf9, 0x88, 0x9e, 0x61, 0x02, 0x0b, 0xf3,
    0x39, 0xb1, 0x60, 0xd6, 0x8e, 0xc6, 0xa0, 0xba, 0x54, 0x12, 0xde, 0x82,
    0xc4, 0x0c, 0x7e, 0xdf, 0x0e, 0x88, 0xfd, 0xce, 0x39, 0x06, 0x74, 0x6e,
    0xd5, 0xaf, 0xcc, 0x8e, 0xd1, 0x5f, 0x77, 0xc0, 0xe3, 0xe6, 0x85, 0x41,
    0xcb, 0x6c, 0xf4, 0x99, 0x19, 0x6c, 0x49, 0x69, 0x3c, 0x02, 0xe9, 0x1f,
    0x02, 0xb6, 0xc9, 0x72, 0xc5, 0xb5, 0xd2, 0x6a, 0x76, 0xf9, 0x88, 0x9e,
    0x61, 0x02, 0x0b, 0xf3, 0x39, 0xb1, 0x60, 0xd6, 0x8e, 0xc6, 0xa0, 0xba,
    0x54, 0x12, 0xde, 0x82, 0xc4, 0x0c, 0x7e, 0xdf, 0x0e, 0x88, 0xfd, 0xce,
    0x39, 0x06, 0x74, 0x6e, 0xd5, 0xaf, 0xcc, 0x8e, 0xd1, 0x5f, 0x77, 0xc0,
    0xe3, 0xe6, 0x85, 0x41, 0xcb, 0x6c, 0xf4, 0x99, 0x19, 0x6c, 0x49, 0x69,
    0x3c, 0x02, 0xe9, 0x1f, 0x02, 0xb6, 0xc9, 0x72, 0xc5, 0xb5, 0xd2, 0x6a,
    0x76, 0xf9, 0x88, 0x9e, 0x61, 0x02, 0x0b, 0xf3, 0x39, 0xb1, 0x60, 0xd6,
    0x8e, 0xc6, 0xa0, 0xba, 0x54, 0x12, 0xde, 0x82, 0xc4, 0x0c, 0x7e, 0xdf,
    0x0e, 0x88, 0xfd, 0xce, 0x39, 0x06, 0x74, 0x6e, 0xd5, 0xaf, 0xcc, 0x8e,
    0xd1, 0x5f, 0x77, 0xc0, 0xe3, 0xe6, 0x85, 0x41, 0xcb, 0x6c, 0xf4, 0x99,
    0x19, 0x6c, 0x49, 0x69, 0x3c, 0x02, 0xe9, 0x1f, 0x02, 0xb6, 0xc9, 0x72,
    0xc5, 0xb5, 0xd2, 0x6a, 0x76, 0xf9, 0x88, 0x9e, 0x61, 0x02, 0x0b, 0xf3,
    0x39, 0xb1, 0x60, 0xd6, 0x8e, 0xc6, 0xa0, 0xba, 0x54, 0x12, 0xde, 0x82,
    0xc4, 0x0c, 0x7e, 0xdf, 0x0e, 0x88, 0xfd, 0xce, 0x39, 0x06, 0x74, 0x6e,
    0xd5, 0xaf, 0xcc, 0x8e, 0xd1, 0x5f, 0x77, 0xc0, 0xe3, 0xe6, 0x85, 0x41,
    0xcb, 0x6c, 0xf4, 0x99, 0x19, 0x6c, 0x49, 0x69, 0x3c, 0x02, 0xe9, 0x1f,
    0x02, 0xb6, 0xc9, 0x72, 0xc5, 0xb5, 0xd2, 0x6a, 0x76, 0xf9, 0x88, 0x9e,
    0x61, 0x02, 0x0b, 0xf3, 0x39, 0xb1, 0x60, 0xd6, 0x8e, 0xc6, 0xa0, 0xba,
    0x54, 0x12, 0xde, 0x82, 0xc4, 0x0c, 0x7e, 0xdf, 0x0e, 0x88, 0xfd, 0xce,
    0x39, 0x06, 0x74, 0x6e, 0xd5, 0xaf, 0xcc, 0x8e, 0xd1, 0x5f, 0x77, 0xc0,
    0xe3, 0xe6, 0x85, 0x41, 0xcb, 0x6c, 0xf4, 0x99, 0x19, 0x6c, 0x49, 0x69,
    0x3c, 0x02, 0xe9, 0x1f, 0x02, 0xb6, 0xc9, 0x72, 0xc5, 0xb5, 0xd2, 0x6a,
    0x76, 0xf9, 0x88, 0x9e, 0x61, 0x02, 0x0b, 0xf3, 0x39, 0xb1, 0x60, 0xd6,
    0x8e, 0xc6, 0xa0, 0xba, 0x54, 0x12, 0xde, 0x82, 0xc4, 0x0c, 0x7e, 0xdf,
    0x0e, 0x88, 0xfd, 0xce, 0x39, 0x06, 0x74, 0x6e, 0xd5, 0xaf, 0xcc, 0x8e,
    0xd1, 0x5f, 0x77, 0xc0, 0xe3, 0xe6, 0x85, 0x41, 0xcb, 0x6c, 0xf4, 0x99,
    0x19, 0x6c, 0x49, 0x69, 0x3c, 0x02, 0xe9, 0x1f, 0x02, 0xb6, 0xc9, 0x72,
    0xc5, 0xb5, 0xd2, 0x6a, 0x76, 0xf9, 0x88, 0x9e, 0x61, 0x02, 0x0b, 0xf3,
    0x39, 0xb1, 0x60, 0xd6, 0x8e, 0xc6, 0xa0, 0xba, 0x54, 0x12, 0xde, 0x82,
    0xc4, 0x0c, 0x7e, 0xdf, 0x0e, 0x88, 0xfd, 0xce, 0x39, 0x06, 0x74, 0x6e,
    0xd5, 0xaf, 0xcc, 0x8e, 0xd1, 0x5f, 0x77, 0xc0, 0xe3, 0xe6, 0x85, 0x41,
    0xcb, 0x6c, 0xf4, 0x99, 0x19, 0x6c, 0x49, 0x69, 0x3c, 0x02, 0xe9, 0x1f,
    0x02, 0xb6, 0xc9, 0x72, 0xc5, 0xb5, 0xd2, 0x6a, 0x76, 0xf9, 0x88, 0x9e,
    0x61, 0x02, 0x0b, 0xf3, 0x39, 0xb1, 0x60, 0xd6, 0x8e, 0xc6, 0xa0, 0xba,
    0x54, 0x12, 0xde, 0x82, 0xc4, 0x0c, 0x7e, 0xdf, 0x0e, 0x88, 0xfd, 0xce,
    0x39, 0x06, 0x74, 0x6e, 0xd5, 0xaf, 0xcc, 0x8e, 0xd1, 0x5f, 0x77, 0xc0,
    0xe3, 0xe6, 0x85, 0x41, 0xcb, 0x6c, 0xf4, 0x99, 0x19, 0x6c, 0x49, 0x69,
    0x3c, 0x02, 0xe9, 0x1f, 0x02, 0xb6, 0xc9, 0x72, 0xc5, 0xb5, 0xd2, 0x6a,
    0x76, 0xf9, 0x88, 0x9e, 0x61, 0x02, 0x0b, 0xf3, 0x39, 0xb1, 0x60, 0xd6,
    0x8e, 0xc6, 0xa0, 0xba, 0x54, 0x12, 0xde, 0x82, 0xc4, 0x0c, 0x7e, 0xdf,
    0x0e, 0x88, 0xfd, 0xce
};

static void stream_buf_cleanup(struct stream_buf *buf)
{
    if (buf->buffer)
        free(buf->buffer);

    buf->buffer = NULL;
    buf->capacity = 0;
    buf->len = 0;
}

static void gtest_extract_data_from_envelop(void)
{
    BIO *bio = NULL;
    RSA *rsa = NULL;
    EVP_PKEY *key = NULL;
    struct stream_buf respon_data = {0};
    struct stream_buf plain_text;
    struct rsa_keypair* keypair;
    unsigned int rc;

    respon_data.buffer = g_base64_decode(enveloped_data_enbase64, (gsize *)&respon_data.len);
    respon_data.capacity = respon_data.len;

    plain_text.capacity = plain_text.len = sizeof(expected_data);
    plain_text.buffer = g_malloc0(plain_text.capacity);

    keypair = g_malloc0(sizeof(struct rsa_keypair));

    /* load RSA private key */
    bio = BIO_new_mem_buf(rsa_private_key, -1);
    g_assert(bio);

    rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL);
    g_assert(rsa);

    /* convert rsa key to evp_pkey */
    key = EVP_PKEY_new();
    g_assert(key);
    EVP_PKEY_assign_RSA(key, rsa);
    keypair->keys = key;

    rc = extract_data_from_envelop(respon_data.buffer, respon_data.len, keypair, &plain_text);
    g_assert_cmpint(rc, ==, CMS_SUCCESS);

    g_assert_cmpmem(plain_text.buffer, plain_text.len, expected_data, sizeof(expected_data));

    stream_buf_cleanup(&respon_data);
    stream_buf_cleanup(&plain_text);
    g_free(keypair);
    BIO_free(bio);
    EVP_PKEY_free(key);
}

int main(int argc, char *argv[])
{
    g_test_init(&argc, &argv, NULL);

    g_test_add_func("/kms/extract_data_from_envelop", gtest_extract_data_from_envelop);

    return g_test_run();
}

